package mysql

import (
	//"fmt"
	_ "github.com/go-sql-driver/mysql"
	"github.com/xormplus/core"
	"github.com/xormplus/xorm"
	//"os"
)
type MysqlSink struct {
	database *xorm.Engine
}

//func NewMysqlSink() (sink MysqlSink, err error) {
//	dbConnection := fmt.Sprintf("%s:%s@tcp(%s%s)/%s", DBUser, DBPwd, DBHost, DBPort,
//		DBDatabase)
//	db, err := sql.Open("mysql", dbConnection)
//	if err != nil {
//		return
//	}
//	sink = MysqlSink{}
//	sink.database = db
//	return
//}

func (sink MysqlSink) Close() {
	sink.database.Close()
}

func NewMysqlSink()(sink MysqlSink, err error)  {
	engine, err := xorm.NewEngine("mysql", "root:root@/go?charset=utf8")
	if err != nil{
		panic(err.Error())
	}
	//defer engine.Close() //engine可以通过engine.Close来手动关闭，但是一般情况下可以不用关闭，在程序退出时会自动关闭。
	engine.ShowSQL(true)//在控制台打印出生成的SQL语句；
	tbMapper := core.NewPrefixMapper(core.SnakeMapper{}, "m_")
	engine.SetTableMapper(tbMapper)
	//engine.Logger().SetLevel(core.LOG_DEBUG) //在控制台打印调试及以上的信息；
	sink = MysqlSink{}
	sink.database = engine
	//无效 回头找原因
	//err1 := engine.Sync2(new(Singer))
	//fmt.Println("我的天哪",err1)
	return
	//保存到文件
	//f, err := os.Create("sql.log")
	//if err != nil {
	//	println(err.Error())
	//	return
	//}
	//engine.SetLogger(xorm.NewSimpleLogger(f))
	//engine.SetMaxIdleConns(2) //连接池
	//engine.SetMaxOpenConns(3)//最大打开连接数
}